#include "wasm_utils.h"
#include <time.h>
#include <string.h>
#include "rtp.h"

const unsigned int PAYLOAD_TYPE_VIDEO_H264 = 0x80 | 0x62;
const unsigned int PAYLOAD_TYPE_VIDEO_H265 = 0x80 | 0x63;
const unsigned int PAYLOAD_TYPE_AUDIO_AAC = 0x80 | 0x60;

//从流中读取dinartp头
unsigned int readDinaRtpHead(unsigned char *buf, int bufSize, DinaRtpHead *outRtpHead)
{
    const int rtpHeadSize = 16;

    if (rtpHeadSize > bufSize)
    {
        return false;
    }
    memset(outRtpHead, 0, rtpHeadSize);
    unsigned char hs[rtpHeadSize];
    memcpy(hs, buf, rtpHeadSize);
    int idx = 0;
    const int idxV0 = idx;
    idx++;
    const int idxV1 = idx;
    idx++;
    const int idxPS0 = idx;
    idx++;
    const int idxPS1 = idx;
    idx++;
    const int idxT = idx;
    idx++;
    const int idxPT = idx;
    idx++;
    const int idxSN0 = idx;
    idx++;
    const int idxSN1 = idx;
    idx++;

    const int idxTS0 = idx;
    idx++;
    const int idxTS1 = idx;
    idx++;
    const int idxTS2 = idx;
    idx++;
    const int idxTS3 = idx;
    idx++;

    const int idxSSRC0 = idx;
    idx++;
    const int idxSSRC1 = idx;
    idx++;
    const int idxSSRC2 = idx;
    idx++;
    const int idxSSRC3 = idx;
    idx++;

    outRtpHead->version = (short)hs[idxV0] << 8 | hs[idxV1];
    outRtpHead->rtpPackageSize = (short)hs[idxPS0] << 8 | hs[idxPS1];
    outRtpHead->tag = hs[idxT];
    outRtpHead->payloadType = hs[idxPT];
    outRtpHead->sequenceNumber = (short)hs[idxSN0] << 8 | hs[idxSN1];
    outRtpHead->timestamp = (unsigned int)hs[idxTS0] << 24 | (unsigned int)hs[idxTS1] << 16 | (unsigned int)hs[idxTS2] << 8 | (unsigned int)hs[idxTS3];
    outRtpHead->ssrc = (unsigned int)hs[idxSSRC0] << 24 | (unsigned int)hs[idxSSRC1] << 16 | (unsigned int)hs[idxSSRC2] << 8 | (unsigned int)hs[idxSSRC3];

    return true;
}